package com.example.hao.qq.Fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;


import com.example.hao.qq.Adapter.MyBaseAdapter;
import com.example.hao.qq.Pojo.QingChiFan;
import com.example.hao.qq.R;
import com.example.hao.qq.UserActivity;
import com.example.hao.qq.Utils.HttpUtils;
import com.google.gson.Gson;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class ConversationFragment extends Fragment implements AdapterView.OnItemClickListener {


    private QingChiFan qingChiFan;

    public ConversationFragment() {
        // Required empty public constructor
    }


    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_a, container, false);
        initView();
        loadData();

        return view;
    }

    private ListView lv;
    private void initView() {
        lv = view.findViewById(R.id.lv);
        lv.setOnItemClickListener(this);
    }

    private void loadData() {
        String address = "http://api.qingchifan.com/api/event/city.json?access_token=6916a7ab1f71e43ac4eadf40a51b4ab1E4DA4EE79DE4C64C449D09318FE0784F&basicCheck=0&city=010&multi=0&gender=0&time=0&age=0&constellation=0&occupation=0&start=0&size=20&apiVersion=2.8.0";
        new Worker().execute(address);
    }




    private class Worker extends AsyncTask<String, Void, QingChiFan> {


        @Override
        protected void onPostExecute(QingChiFan qingChiFan) {
            MyBaseAdapter myBaseAdapter = new MyBaseAdapter(getContext(), qingChiFan);
            lv.setAdapter(myBaseAdapter);
        }

        @Override
        protected QingChiFan doInBackground(String... strings) {
            String s = HttpUtils.get(strings[0]);
            qingChiFan = new Gson().fromJson(s, QingChiFan.class);

            return qingChiFan;
        }
    }

    QingChiFan.DataBean.ResultsBean.UserBean user;
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        List<QingChiFan.DataBean.ResultsBean> results = qingChiFan.getData().getResults();
        user = results.get(position).getUser();
        new Thread(new Runnable() {
            @Override
            public void run() {
                transmit();
            }
        }).start();
    }

    //传输数据
    private void transmit() {
        String userImageUrl = user.getUserImageUrl();//头像
        String constellation = user.getConstellation();//星座
        int gender = user.getGender();//性别
        int age = user.getAge();//年龄
        int height = user.getHeight();//身高
        String nick = user.getNick();//昵称
        Intent intent = new Intent(getActivity(),UserActivity.class);
        intent.putExtra("头像", userImageUrl);
        String NICK = "昵称:"+nick;
        intent.putExtra("昵称", NICK);
        String AGE = age+"岁";
        intent.putExtra("年龄", AGE);
        intent.putExtra("性别", gender == 1 ? "男":"女");
        String HEIGHT = "身高"+height;
        intent.putExtra("身高", HEIGHT);
        intent.putExtra("星座", constellation);
        startActivity(intent);
    }
}
